<template>
  <view class="container">
    <view class="header">
      <up-search placeholder="搜索门店名称" v-model="searchForm.title" :show-action="false" @clear="handleSearch" @search="handleSearch"></up-search>
    </view>
    <view class="content">
      <f-list ref="listRef" :request="getData">
        <template #header>
          <view class="total">
            共筛选出<text class="text-[red]">{{total}}</text>条记录
          </view>
        </template>
        <template #default="{item, index}">
          <com-store-item :item="item" @change="handleChange(item, index)" @transfer="handleTransfer(item, index)"></com-store-item>
        </template>
      </f-list>
    </view>
    <view class="footer">
      <up-button class="w-full" plain type="primary" size="small" @click=handleAdd>添加门店</up-button>
    </view>
      
    <!-- 转移用户 -->
    <com-store-popup ref="storePopupRef" @confirm="handleTransferConfirm"></com-store-popup>
    <!-- 添加门店 -->
    <com-add-store ref="storeAddRef" @confirm="handleAddConfirm"></com-add-store>
  </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import FList from '@/addon/htscrm/components/f-list/index.vue';
import ComStoreItem from './com-store-item.vue';
import ComAddStore from './com-add-store.vue';
import ComStorePopup from './com-store-popup.vue';
import { getStoreListApi, getStoreLoginInfoApi, transferStoreUserApi } from '@/addon/htscrm/api';
import useAdminStore from '@/addon/htscrm/stores/admin';
import { redirect } from '@/utils/common';

const adminStore = useAdminStore()

const searchForm = ref<any>({
  title: '',
})

const total = ref(0);
const storePopupRef = ref()
const storeAddRef = ref()
const listRef = ref()
const current = ref<IStoreItem>()

async function getData(meta: any) {
  const params = {
    ...searchForm.value,
    ...meta
  }
  const res = await getStoreListApi(params)
  
  if (res.code == 1) {
    total.value = res.data.total
    return res.data
  }
}

/**
 * 搜索
 */
 function handleSearch() {
  listRef.value?.reload();
}

async function handleChange(item: IStoreItem, index: number) {
  const res = await getStoreLoginInfoApi(item.admin_id)
  if (res.code == 1) {
    adminStore.setAdminToken(res.data.token)
		adminStore.setUserInfo(res.data.userinfo)
		uni.showToast({title:'登录成功',icon:"none"})
		redirect({
			url:"/addon/htscrm/pages/admin/index",
      mode: 'redirectTo'
		})
  }
}

function handleTransfer(item: IStoreItem, index: number) {
  storePopupRef.value.open(item)
  current.value = item
}

async function handleTransferConfirm(e) {
  if(e?.id && current.value?.id) {
    const res = await transferStoreUserApi({
      new_store_id: e?.id,
      old_store_id: current.value?.id,
    })
    if(res.code === 1) {
      uni.showToast({title:'转移成功',icon:"none"})
      storePopupRef.value.close()
    }
  }

  
}

function handleAdd() {
  storeAddRef.value.open()
}

function handleAddConfirm(e:any) {
  listRef.value?.reload();
}

</script>

<style scoped lang="scss">
.container {
  min-height: 100vh;
  background-color: #f7f7f7;
  padding-bottom: 96rpx;

  .header {
    padding: 20rpx 30rpx;
    background-color: #fff;
  }

  .total {
    padding: 20rpx 30rpx 0;
  }

  .footer {
    width: 100vw;
    box-sizing: border-box;
    position: fixed;
    bottom: 0;
    left: 0;
    background-color: #fff;
    padding: 20rpx 30rpx;
    display: flex;

    .up-button {
      flex: 1;
    }
  }
}
</style>